//
//  JWKWelcomeViewController.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/23.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit
import SDWebImage

class JWKWelcomeViewController: UIViewController {
    //设置图片的初始位置系数
    private let icontop = 0.65*UIScreenHeight
    
    
    //懒加载子控件
    //背景图片框
    lazy var bgImageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像图片框
    lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //昵称文案
    //如果属性提前指定了类型,那么闭包中可以不声明返回值类型
    lazy var nameLabel : UILabel = {
        let l = UILabel()
        
        l.text = JWKUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name
        l.font = UIFont.systemFontOfSize(16)
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        l.numberOfLines = 0
        l.sizeToFit()
        return l
    }()
    
    //设置约束
    //在初始时候就设置约束  不要在viewDidLayoutSubviews中设置约束
    //系统是根据提前指定好的约束 在viewDidLayoutSubviews方法中根据约束修改frame
    private func setupUI(){
        //添加子控件
        view.addSubview(iconImageView)
        view.addSubview(nameLabel)
        

        
        iconImageView.snp_makeConstraints { (make) in
            

            //用bottom这样设置是不对的,导致y很大,并不能达到预想的0.7*UIScreenHeight
//            make.bottom.equalTo(0.7*UIScreenHeight)
            
            //可以换成-make.bottom.equalTo(view).offset(-0.2*UIScreenHeight)
            //也可以用top设置固定的数值
            make.top.equalTo(view).offset(icontop)
//            make.bottom.equalTo(view).offset(-160)
            make.centerX.equalTo(view)
            make.width.equalTo(90)
            make.height.equalTo(90)
            
           
            
        }
        //加载图片
        //
        //            let imageStr : String = (JWKUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large)!
        //
        //            let imageicon = UIImage(named: imageStr)
        //
        //为啥image总是nil????!!因为这个图片名字是网络的,没有加载到本地!
        
        
        
        iconImageView.sd_setImageWithURL(JWKUserAccountViewModel.sharedUserAccountViewModel.iconUrl)
        //            print(iconImageView.image?.size)
        
        nameLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(iconImageView.snp_bottom).offset(8)
            make.centerX.equalTo(bgImageView)
            
        }
        
        //设置属性
        nameLabel.alpha = 0


    }
    
//    override func loadView() {
//        view = bgImageView
//    }

    override func viewDidLoad() {
//        super.viewDidLoad()

        setupUI()
        
    }
    
    override func loadView() {
        super.loadView()
        view  = bgImageView
        //防止bgimageview也跟着做动画
    }

    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        
        
//        print(iconImageView.image?.size)为什么图片总是nil但是又能显示出来,尺寸,缩放也都是nil---SD没有加载出来,还是要在viewDidAppear结束之后才可以获取
        
        startAnimation()
    }
    
    
    func startAnimation(){
        //修改约束 加入动画
        //弹簧动画效果
        //usingSpringWithDamping: 阻尼系数 0 ~ 1 值越小 效果越弹
        //initialSpringVelocity : 加速度 值越大 速度越快
        //OC:位移枚举 表示多个动画选项 '|' swift: 使用数组数组
        let offset = UIScreenHeight - 1.25*icontop
        self.iconImageView.snp_updateConstraints(closure: { (make) in
      
            
            make.top.equalTo(view).offset(offset)
            //TODO:要刷新视图 不能放到设置约束里面!而且update是刷新某一条约束,其余的都不变
//            self.view.layoutIfNeeded()
        })
        
        
         UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: [], animations: {
            //刷新视图更新frame
            self.view.layoutIfNeeded()
            }) { (_) in
                
                //显示label

                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
                        
                        //更改根视图控制器--主界面
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "Main")
                })
        }
        
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
//        print(iconImageView.image)
//        print(iconImageView.frame)
//        print(iconImageView.image?.size,iconImageView.image?.scale)
        //在这里获取的图片的尺寸就是180*180,不是90*90,6s模拟器,缩放比例1.0,但是网页上的图片确实是180像素
//        Optional(<UIImage: 0x7feae9d883d0>, {180, 180})
//        (142.5, 433.5, 90.0, 90.0)
//        Optional((180.0, 180.0)) Optional(1.0)
        
//        let image = UIImage(named: "avatar_default_big")
//        print(image)
//        print(iconImageView.frame)
//        print(image!.size,image!.scale)
//        Optional(<UIImage: 0x7fe542833a60>, {85, 85})
//        (85.0, 85.0) 2.0
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}



